package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.SaleOutDepotCriteria;
import com.javasm.domin.entity.SaleOutDepot;
import com.javasm.domin.entity.SodDetailConn;
import com.javasm.domin.entity.SodSn;
import com.javasm.domin.vo.SaleOutDepotVo;
import com.javasm.domin.vo.SodSnVo;
import com.javasm.mapper.SaleOutDepotMapper;
import com.javasm.service.SaleOutDepotService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.SaleOutDepotTransfer;
import com.javasm.transfer.SodSnTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whl
 * @since 2022-01-17
 */
@Service
@Transactional
@RequiredArgsConstructor
public class SaleOutDepotServiceImpl extends BaseServiceimpl<SaleOutDepot> implements SaleOutDepotService {

    private final SaleOutDepotMapper saleOutDepotMapper;
    private final SaleOutDepotTransfer saleOutDepotTransfer;
    private final SodSnTransfer sodSnTransfer;

    @Override
    public PageResult<SaleOutDepotVo> serchPage(SaleOutDepotCriteria criteria) {
//        System.out.println("criteria -=-=-=-");
        System.out.println(criteria);
        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());
        LambdaQueryWrapper<SaleOutDepot> lambda = new QueryWrapper<SaleOutDepot>().lambda();

        if (!StringUtils.isEmpty(criteria.getId())) {
            lambda.like(SaleOutDepot::getId, criteria.getId());
        }
        if (!StringUtils.isEmpty(criteria.getSodStatus())) {
            lambda.eq(SaleOutDepot::getSodStatus, criteria.getSodStatus());
        }
        if (!StringUtils.isEmpty(criteria.getStartTime())) {
            lambda.between(SaleOutDepot::getSodTime, criteria.getStartTime(), criteria.getEndTime());
        }
        if (!StringUtils.isEmpty(criteria.getIndentType())) {
            lambda.eq(SaleOutDepot::getIndentType, criteria.getIndentType());
        }
        if (!StringUtils.isEmpty(criteria.getVipNumber())) {
            lambda.like(SaleOutDepot::getVipNumber, criteria.getVipNumber());
        }
        if (!StringUtils.isEmpty(criteria.getPayType())) {
            lambda.eq(SaleOutDepot::getPayType, criteria.getPayType());
        }

        List<SaleOutDepot> saleOutDepots = saleOutDepotMapper.selectTable(lambda);
//        System.out.println("saleOutDepots -=-=-");
        System.out.println(saleOutDepots);
        PageInfo<SaleOutDepot> pageInfo = new PageInfo<>(saleOutDepots);
        List<SaleOutDepotVo> saleOutDepotVos = saleOutDepotTransfer.setSodStatus(saleOutDepots);
//        System.out.println("total:" + pageInfo.getTotal());
//        System.out.println("saleOutDepotVos -=-=-");
        System.out.println(saleOutDepotVos);

        return new PageResult<SaleOutDepotVo>(pageInfo.getTotal(), saleOutDepotVos);
    }

    @Override
    public SaleOutDepotVo getSodById(Long id) {
        SaleOutDepot sodById = saleOutDepotMapper.getSodById(id);
//        System.out.println("sodById ====>");
//        System.out.println(sodById);
        ArrayList<SaleOutDepot> sodByIdList = new ArrayList<>();
        sodByIdList.add(sodById);
//        System.out.println("sodByIdList ===>");
//        System.out.println(sodByIdList);
        SaleOutDepotVo sodVo = saleOutDepotTransfer.setSodStatus(sodByIdList).get(0);
//        System.out.println("getSodById====>");
//        System.out.println(sodVo);
        return sodVo;
    }

    @Override
    public List<SodDetailConn> getDetail(Long id) {
        List<SodDetailConn> sodDetail = saleOutDepotMapper.getSodDetail(id);
        return sodDetail;
    }

    @Override
    public List<SodSnVo> getSodSn(Long id) {
        List<SodSn> sodSnList = saleOutDepotMapper.getSodSnById(id);
        System.out.println("-=-=-=- SN -=-=-=");
        System.out.println(sodSnList);
        return sodSnTransfer.setSodSn(sodSnList);
    }
}
